<!DOCTYPE html>
<html lang="CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书列表展示</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">

    <link rel="stylesheet" href="css/list.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script src="js/jq-paginator.js"></script>
</head>

<body>
    <div class="bookContainer">
        <h2>图书列表展示</h2>
        <div class="navbar-justify-between">
            <div>
                <button class="btn btn-outline-info" type="button" onclick="location.href='book_add.html'">添加图书</button>
                <button class="btn btn-outline-info" type="button" onclick="batchDelete()">批量删除</button>
            </div>
        </div>

        <table>
            <thead>
                <tr>
                    <td>选择</td>
                    <td class="width100">图书ID</td>
                    <td>书名</td>
                    <td>作者</td>
                    <td>数量</td>
                    <td>定价</td>
                    <td>出版社</td>
                    <td>状态</td>
                    <td class="width200">操作</td>
                </tr>
            </thead>
            <tbody></tbody>
        </table>

        <div class="demo">
            <ul id="pageContainer" class="pagination justify-content-center"></ul>
        </div>
        <script>
            getPage()
            //翻页信息 第一次登录后会直接获取第一页的信息

            function getPage() {
                $.ajax({
                    url: "/library/getPage"+location.search ,
                    type: "POST",
                    // data: ,
                    success: function (result) {
                        console.log("result=" + result);
                        // === 不转换比较 "5"===5->false 而 "5"==5->true
                        console.log("connection = " + result.connection);
                        if (result==null||result.success == false || result.data == null) {
                            console.log("请求错误||未登录");
                            // location.href="login.html";
                            return;
                        }

                        var htmls = "";
                        for (var s of result.data.books) {
                            htmls += '<tr>';
                            htmls += '<td><input type="checkbox" name="selectBook" value="' + s.id + '" id="selectBook" class="book-select"></td>';
                            htmls += '<td>' + s.id + '</td>';
                            htmls += '<td>' + s.name + '</td>';
                            htmls += '<td>' + s.author + '</td>';
                            htmls += '<td>' + s.count + '</td>';
                            htmls += '<td>' + s.price + '</td>';
                            htmls += '<td>' + s.publisher + '</td>';
                            htmls += '<td>' + s.stateCN + '</td>';
                            htmls += '<td>';
                            htmls += '<div class="op">';
                            htmls += '<a href="book_update.html?bookId=' + s.id + '">修改</a>';
                            htmls += '<a href="javascript:void(0)" onclick="deleteBook(' + s.id + ')">删除</a>';
                            htmls += '</div>';
                            htmls += '</td>';
                            htmls += '</tr>';
                        }
                        $("tbody").html(htmls);

                        $("#pageContainer").jqPaginator({
                            totalCounts: result.data.totalPages, //总记录数
                            pageSize: 10,    //每页的个数
                            visiblePages: 5, //可视页数
                            currentPage: result.data.currentPage,  //当前页码的位置
                            first: '<li class="page-item"><a class="page-link">首页</a></li>',
                            prev: '<li class="page-item"><a class="page-link" href="javascript:void(0);">上一页<\/a><\/li>',
                            next: '<li class="page-item"><a class="page-link" href="javascript:void(0);">下一页<\/a><\/li>',
                            last: '<li class="page-item"><a class="page-link" href="javascript:void(0);">最后一页<\/a><\/li>',
                            page: '<li class="page-item"><a class="page-link" href="javascript:void(0);">{{page}}<\/a><\/li>',
                            //页面初始化和页码点击时都会执行
                            onPageChange: function (page, type) {
                                console.log("第" + page + "页, 类型:" + type);
                                if (type == "change") {
                                    location.href = "book_list.html?currentPage="+page;
                                }
                            }
                        });
                    },
                    error: function (error){
                        console.log("401");
                        console.log(error.errMsg);
                        if(error.status==401){
                            location.href = "/library/unLogin.html";
                        }
                    }
                });
            }

            // function getBookList() {
            //     //返回服务器储存的book信息
            //     $.ajax({
            //         url: "/library/getList",
            //         type: "GET",
            //
            //         success: function(result){
            //             var htmls = "";
            //             for(var s of result){
            //                 htmls+='<tr>';
            //                 htmls+='<td><input type="checkbox" name="selectBook" value="'+s.id+'" id="selectBook" class="book-select"></td>';
            //                 htmls+='<td>'+s.id+'</td>';
            //                 htmls+='<td>'+s.name+'</td>';
            //                 htmls+='<td>'+s.author+'</td>';
            //                 htmls+='<td>'+s.count+'</td>';
            //                 htmls+='<td>'+s.price+'</td>';
            //                 htmls+='<td>'+s.publisher+'</td>';
            //                 htmls+='<td>'+s.stateCN+'</td>';
            //                 htmls+='<td>';
            //                 htmls+='<div class="op">';
            //                 htmls+='<a href="book_update.html?bookId='+s.id+'">修改</a>';
            //                 htmls+='<a href="javascript:void(0)" onclick="deleteBook('+s.id+')">删除</a>';
            //                 htmls+='</div>';
            //                 htmls+='</td>';
            //                 htmls+='</tr>';
            //             }
            //             $("tbody").html(htmls);
            //         },
            //         error: function(xhr,status,err){
            //
            //         }
            //     });
            // }


            function deleteBook(id) {
                var isDelete = confirm("确认删除?");
                if (isDelete) {
                    //删除图书
                    $.ajax({
                    url:"/library/delBook",
                    type:"GET",
                    data:"id="+id,
                });
                    alert("删除成功");
                    getPage()
                }
            }

            function batchDelete() {
                var isDelete = confirm("确认批量删除?");
                if (isDelete) {
                    //获取复选框的id
                    var ids = [];
                    $("input:checkbox[name='selectBook']:checked").each(function () {
                        ids.push($(this).val());
                    });
                    console.log(ids);
                    $.ajax({
                        url: "/library/dels",
                        type: "POST",
                        data: JSON.stringify({"bookId":ids}),
                        contentType: "application/json",
                        success: function(result) {
                            console.log(result.success);
                            // location.href = "book_list.html";
                            getPage();
                        },
                        error: function (error){
                            console.log("401");
                            if(error.status==401){
                                location.href = "/library/unLogin.html";
                            }
                        }
                    });
                    alert("批量删除成功");
                }
            }
        </script>
    </div>
</body>

</html>